<template>
  <a-modal
    :visible="visible"
    title="新增"
    cancelText="取消"
    okText="提交"
    width="60%"
    @ok="submit"
    @cancel="cancel"
  >
    <a-form
      ref="formRef"
      :model="formState.state"
      :rules="formRules"
      :label-col="labelCol"
      :wrapper-col="wrapperCol"
    >
      <a-row>
        <a-col :span="12">
          <a-form-item label="跟进人姓名" name="createBy">
            <a-input readonly v-model:value="formState.state.createBy" placeholder="请选择">
              <template #addonAfter>
                <div
                  class="primary-color cursor-pointer"
                  @click="openDialog()"
                >
                  {{ '选择' }}
                </div>
              </template>
            </a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="下次跟进时间" name="createTime">
            <a-date-picker v-model:value="formState.state.createTime" valueFormat="YYYY-MM-DD hh:mm:ss" show-time placeholder="请选择" style="width: 100%"/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="跟进进度" name="followUpProgress">
            <a-select v-model:value="formState.state.followUpProgress" placeholder="请选择">
              <a-select-option v-for="item in dictLists.data[dictEnum.FOLLOW_PROGRESS]"
                                :key="item.id" :value="item.value"
              >{{ item.label }}</a-select-option>
            </a-select>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="备注" name="remark">
            <a-textarea
              v-model:value="formState.state.remark"
              placeholder="请输入"
              maxlength="100"
              :auto-size="{ minRows: 2, maxRows: 5 }"
            />
          </a-form-item>
        </a-col>
      </a-row>
    </a-form>
    <BasicTableModal
      title="课程选择"
      v-model:visible="modalConfig.visiableUser"
      :columns="modalConfig.columns"
      :fetchAjax="modalConfig.api"
      :searchForm="modalConfig.searchForm"
      @selectUniversity="handleSelectModal"
    />
  </a-modal>
</template>
<script setup>
import BasicTableModal from "@/component/basic-table-modal";
import {onMounted, reactive, ref, watch, toRaw, defineEmits, defineProps } from "vue";
import { getDictByCodesReturnObj } from '@/tools/get-dict'
import dictEnum from '@/const/dict'
import { mainUser } from "@/api/module/channel-manage";
import { updateFollowUpRecord, putFollowUpRecord } from '@/api/module/school'
import {message} from "ant-design-vue";
import { useI18n } from "vue-i18n";

const { t } = useI18n();
const emit = defineEmits(['close'])
const props = defineProps({
  parent: {
    type: Object,
    default: () => {}
  },
})
let isLoading = ref(false)
const formRef = ref();
const formState = reactive({
  state: {
    createBy: '',
    followUpProgress: '',
    createTime: '',
    schId: props.parent.id || '',
  }
});
const modalConfig = reactive({
  visiableUser: false,
  columns: [
    { dataIndex: "nickname", key: "nickname", title: t('activityManage.label.active-16') },
    { dataIndex: "deptName", key: "deptName", title: t('activityManage.label.active-17') },
    { dataIndex: "postName", key: "postName", title: t('activityManage.label.active-18') },
  ],
  dataIndex: null,
  searchForm: [
    { type: 'input', key: "nickname", label: t('activityManage.label.active-16') },
  ],
  api: mainUser
});
const openDialog = () => {
  modalConfig.visiableUser = true
}
const handleSelectModal = (data) => {
  formState.state.createBy = data.nickname
}
const saveKey = "save";
const labelCol = { span: 6 }
const wrapperCol = { span: 18 }
let dictLists = reactive({
  data: {}
})

const formRules = {
  createBy: [ { required: true, message: '请输入联系人姓名', trigger: 'blur'} ],
  createTime: [ { required: true, type: 'date', message: '请选择时间', trigger: 'blur'} ],
  followUpProgress: [ { required: true, message: '请选择跟进进度', trigger: 'blur'} ],
};


const submit = (e) => {
  formRef.value
    .validate()
      .then(() => {
        if (formState.state.id) {
          putFollowUpRecord(toRaw(formState.state)).then((response)=>{
            if(response.success){
              message.success({ content: '保存成功', duration: 1 }).then(()=>{
                cancel();
              });
            }else{
              message.warning({ content: '保存失败', duration: 1 }).then(()=>{
                cancel();
              });
            }
          });
        } else {
          updateFollowUpRecord(toRaw(formState.state)).then((response)=>{
            if(response.success){
              message.success({ content: '保存成功', duration: 1 }).then(()=>{
                cancel();
              });
            }else{
              message.warning({ content: '保存失败', duration: 1 }).then(()=>{
                cancel();
              });
            }
          });
        }
      })
      .catch(error => {
        console.log('error', error);
    });
};
const cancel = (e) => {
  formRef.value.resetFields();
  emit("close", formState.state);
};

onMounted(async () => {
  dictLists.data = await getDictByCodesReturnObj([
    dictEnum.FOLLOW_PROGRESS,
  ])

})


</script>
<style lang="">

</style>
